package com.skyworthclub.competition.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.skyworthclub.competition.beans.User;
import com.skyworthclub.competition.beans.dto.ResetPasswordDto;
import com.skyworthclub.competition.beans.dto.UserRegisterDto;
import com.skyworthclub.competition.mapper.UserMapper;
import com.skyworthclub.competition.service.UserRedisCacheService;
import com.skyworthclub.competition.service.UserService;
import com.skyworthclub.competition.utils.cache.UserKeyGenerator;
import com.skyworthclub.competition.utils.service.PasswordEncoderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author li
 * @since 2018-10-15
 */
@CacheConfig(cacheNames = "UserInfo")
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    /**
     * 默认头像图片地址
     */
    private static final String AVATAR_URL = "http://competition.lzlbog.club/static/upload/images/avatar/li_avatar.jpg";

    private UserMapper userMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper){
        this.userMapper = userMapper;
    }

    @Override
    public boolean register(UserRegisterDto userRegisterDto) {
        User user = new User();
        user.setUsername(userRegisterDto.getUsername());
        user.setPassword(PasswordEncoderUtil.passwordEncode(userRegisterDto.getPassword()));
        user.setAvatarUrl(AVATAR_URL);
        user.setEmail(userRegisterDto.getEmail());
        return super.insert(user);
    }

    @Override
    public boolean updatePassword(ResetPasswordDto dto) {
        User user = new User();
        user.setPassword(PasswordEncoderUtil.passwordEncode(dto.getNewPassword()));
        EntityWrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("email", dto.getEmail());
        return super.update(user, wrapper);
    }

    @Override
    public boolean updateAvatarUrl(String username, String avatarUrl) {
        User user = new User();
        user.setUsername(username);
        user.setAvatarUrl(avatarUrl);
        boolean update = super.update(user, new EntityWrapper<User>().eq("username", username));
        String key = UserKeyGenerator.generate(user);
        redisCacheService.setField(key, "avatarUrl", avatarUrl);
        return update;
    }

    @Override
    public User getUserInfo(String username) {

        //查询缓存
        Optional<User> optional = redisCacheService.get(username);
        if (optional.isPresent()) {
            return optional.get();
        }
        // 查询数据库
        User user;
        EntityWrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("username", username);
        user = super.selectOne(wrapper);
        if (user != null) {
            redisCacheService.set(null, user);
        }
        return user;

    }


    private UserRedisCacheService redisCacheService;

    @Autowired
    public void setRedisCacheService(UserRedisCacheService redisCacheService) {
        this.redisCacheService = redisCacheService;
    }

    @Override
    public boolean addFav(String username, int matchId) {
        try {
            return userMapper.addFav(username,matchId);
        }catch (Exception e){
            //重复添加则返回false
            return false;
        }
    }

    @Override
    public List<Integer> listFavMatchId(String username) {
        return userMapper.listFavMatchId(username);
    }

    @Override
    public boolean removeFav(String username, int matchId) {
        try{
            return userMapper.removeFav(username,matchId);
        }catch (Exception e){
            return false;
        }
    }

}
